package com.fjnu.controller.repair;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.fjnu.controller.common.BaseController;
import com.fjnu.dao.impl.ExamRepairDao;
import com.fjnu.entities.BaseRepair;
import com.fjnu.entities.ExamRepair;
import com.fjnu.entities.MainInstrument;
import com.fjnu.entities.User;
import com.fjnu.service.IBaseRepairService;
import com.fjnu.service.IExamRepairService;
import com.fjnu.service.InstrumentService;

@Controller
public class BaseRepairController extends BaseController{
	private static Logger logger =LogManager.getLogger(BaseRepairController.class);
	@Autowired
	private IBaseRepairService baseRepairService;
	@Autowired	
	private InstrumentService instrumentservice;
	@Autowired
	private IExamRepairService examRepairService;
	/**
	 * 查找当前用户所申请的报修单
	 * @return
	 */
	@RequiresRoles("普通教职工")
	@RequestMapping(value="/findUserRepair")
	@ResponseBody
	public Map<String,Object> findUserRepair(){
		logger.info("普通用户查找报修单");
		if(!init().get(STATUS).equals(SUCCESS))
			return getData2Result();
		Subject currentUser = SecurityUtils.getSubject();  
		 Session session = currentUser.getSession();
		User user=(User) session.getAttribute("subject");
		List<BaseRepair> list=baseRepairService.findUserRepair(user);
		for(BaseRepair baseRepair:list){
			if(baseRepair.getStatus()==baseRepair.NOEXAM){
				baseRepair.setStatusQue("待审核");
			}
			else if(baseRepair.getStatus()==baseRepair.NOPASS){
				baseRepair.setStatusQue("审核未通过");
			}
			else if(baseRepair.getStatus()==baseRepair.NOREPAIR){
				baseRepair.setStatusQue("审核通过,等待维修");
			}else if(baseRepair.getStatus()==baseRepair.SUCCESS){
				baseRepair.setStatusQue("维修完成");
			}
			else {
				baseRepair.setStatusQue("维修失败");
			}
		}
		put("data",list);
		return getData2Result();
	}
	/**
	 * 当前用户申请报修单并向审核部门提交报修单
	 * @param instrumentId
	 * @param repairReason
	 * @return
	 */
	@RequestMapping(value="/addUserRepair")
	@ResponseBody
	public Map<String,Object> addUserRepair(int instrumentId,String repairReason){
		logger.info("申请报修单");
		if(!init(instrumentId).get(STATUS).equals(SUCCESS))
			return getData2Result();
		Subject currentUser = SecurityUtils.getSubject();  
		 Session session = currentUser.getSession();
		User user=(User) session.getAttribute("subject");
		MainInstrument mainInstrument=instrumentservice.findUniqueInstrument(instrumentId);
		mainInstrument.setStatusQuo(mainInstrument.Repair);
		instrumentservice.updateInstrument(mainInstrument);
		Date repairTime=new Date();
		Integer status = 0;
		BaseRepair baseRepair=
				new BaseRepair(null,mainInstrument,user,status,repairTime,null,repairReason);
		baseRepairService.addRepair(baseRepair);
		
		//报修成功后立即向审核部门提交申请单
		logger.info("报修完成，向审核部门提交申请单");
		ExamRepair examRepair=new ExamRepair(null,null,baseRepair);
		examRepairService.addExamRepair(examRepair);
		return getData2Result();
	}
	@RequestMapping(value="/upUserRepair")
	@ResponseBody
	public Map<String,Object> updateUserRepair(int instrumentId,String repairReason){
		logger.info("修改报修单");
		if(!init(instrumentId).get(STATUS).equals(SUCCESS))
			return getData2Result();
		baseRepairService.updateRepair(repairReason, instrumentId);
		return getData2Result();
	}
}
